<?php

namespace App\Core\Traits;

trait HasProps
{
    /**
     * 设置 prop
     * @param string $prop
     * @param string $value
     * @return $this
     */
    public function prop(string $prop, mixed $value): static
    {
        $this->props[$prop] = $value;
        return $this;
    }

    /**
     * 设置 prop
     * @param array $props
     * @param bool $isMerge
     * @return $this
     */
    public function props(array $props, bool $isMerge = true): static
    {
        if ($isMerge){
            $this->props = array_merge($this->props, $props);
        }else{
            $this->props = $props;
        }
        return $this;
    }

    /**
     * 删除 prop
     * @param string $prop
     * @return $this
     */
    public function removeProp(string $prop): static
    {
        if (isset($this->props[$prop])){
            unset($this->props);
        }
        return $this;
    }

    /**
     * 清空 props 变量
     * @return $this
     */
    public function clearProps(): static
    {
        $this->props = [];
        return $this;
    }


    /**
     * 获取 prop
     * @param string|null $key
     * @return mixed
     */
    public function getProps(string $key = null): mixed
    {
        return $key?($this->props[$key]??null):$this->props;
    }
}
